using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using BaseDatos.Comun;
using BaseDatos;
using Aplicacion.Base.WinForm;

namespace SCHD
{
    public partial class WFMigrarMaestroAreas : TDV.Controles.Base.WinForm.BaseForm_Tipo02
    {
        public WFMigrarMaestroAreas()
        {
            InitializeComponent();
        }

        private void WFMigrarMaestroAreas_Load(object sender, EventArgs e)
        {

        }

        private void BtnAbrir_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFD = new OpenFileDialog();
            openFD.Title = "Seleccionar archivos";
            openFD.Filter = "Todos los archivos (*.xlsx)|*.xlsx";
            openFD.Multiselect = false;
            openFD.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            if (openFD.ShowDialog() == DialogResult.OK)
            {
                txtArchivo.Text = openFD.FileName;
            }
            if (txtHoja.Text.Length > 0)
            {
                BtnImportar.Enabled = true;
            }
        }

        private void BtnImportar_Click(object sender, EventArgs e)
        {

            if (txtHoja.Text.Length > 0)
            {
                //********************************
                //Cargando los campos del Excel
                //********************************
                try
                {
                    DataTable dt = new DataTable();
                    string strconn = null;
                    strconn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + txtArchivo.Text + ";Extended Properties=Excel 8.0";
                    OleDbConnection mconn = new OleDbConnection(strconn);
                    OleDbDataAdapter ad = new OleDbDataAdapter("Select * from [" + txtHoja.Text + "$]", mconn);
                    mconn.Open();
                    ad.Fill(dt);
                    mconn.Close();
                    GrdMaestroAreas.DataSource = dt;
                }
                catch (Exception ex)
                {
                    MensajeBarraEstado(ex.Message.ToString());
                }
            } 
        }

        private void BtnAceptar_Click(object sender, EventArgs e)
        {
            try
            {

            
            foreach (DataGridViewRow row in GrdMaestroAreas.Rows)
            {
                DbParametro[] Parametros = new DbParametro[10];
                Parametros[0] = new DbParametro("pcodigo", row.Cells["Sede"].Value.ToString());
                Parametros[1] = new DbParametro("pcodmare", row.Cells["pri"].Value.ToString());
                Parametros[2] = new DbParametro("pcodare", row.Cells["are"].Value.ToString());
                Parametros[3] = new DbParametro("pcodsare", row.Cells["are1"].Value.ToString());
                Parametros[4] = new DbParametro("pdscmare", row.Cells["MacroArea"].Value.ToString());
                Parametros[5] = new DbParametro("pdscare", row.Cells["SubArea"].Value.ToString());
                Parametros[6] = new DbParametro("pdscsare", row.Cells["SubArea1"].Value.ToString());
                Parametros[7] = new DbParametro("pusuario", AplicacionBase.Aplicacion.Usuario.Login);
                Parametros[8] = new DbParametro("pestacion", AplicacionBase.Aplicacion.Usuario.WorkStation);
                Parametros[9] = new DbParametro("popcion", "UPD01");
                Parametros = ConexionBaseDatos.Conexion.EjecutarComando("SETCHDMIGRARAREA", Parametros);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
            
        }

        private void BtnSalir_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

